<?php

ini_set("display_errors", 1);
require "include/template_manager.inc.php";

if (isset($_REQUEST['name'])) {
    $myTemplate = new Template_Manager("corso", null, true, 2);
    $ok = true;
    if (substr($_REQUEST['name'], -1) == '/') {
        $_REQUEST['name'] = substr_replace($_REQUEST['name'], "", -1);
    }
    
    $where = "";
    if (getLanguage() == 'it') {
        //$name = mysql_real_escape_string($_REQUEST['name']);
        $name = $_REQUEST['name'];
        $where = "`c`.`nome` = '" . $name . "'";
    } else {
        //$name = mysql_real_escape_string($_REQUEST['name']);
        $name = $_REQUEST['name'];
        $where = "`c`.`nome_en` = '" . $name . "'";
    }
    
    $query = "select `i`.`meta_description` as `meta_description`,`i`.`meta_description_en` as `meta_description_en`,`i`.`meta_keywords` as `meta_keywords`,`i`.`meta_keywords_en` as `meta_keywords_en`, "
            . "`u`.`titolo` AS `ptitolo`,`u`.`email` AS `email`,`c`.`id` AS `id`"
            . ",`c`.`nome` AS `nome`,`c`.`nome_en` AS `nome_en`,`i`.`uri` AS `img`,"
            . "concat(`u`.`nome`,' ',`u`.`cognome`) AS `professore`,`c`.`ore` AS `ore`,"
            . "`c`.`descrizione` AS `descrizione`,`c`.`descrizione_en` AS `descrizione_en`,"
            . "`c`.`titolo` AS `titolo`,`c`.`titolo_en` AS `titolo_en`,"
            . "`i`.`descrizione` AS `descr`,`i`.`descrizione_en` AS `descr_en` "
            . "from ((`professori` `u` join `corsi` `c`) join `immagine` `i`) "
            . "where ((`c`.`id_professore` = `u`.`id`) and (`c`.`immagine` = `i`.`id`) and " . $where . ")";   
    
    if (($data = DB_Manager::getInstance()->query($query)) && ($row = $data->fetch_array(MYSQLI_ASSOC))) {
        $name = "";
        $description = "";
        $keywords = "";
        $title = "";
        $descr = "";
        $courseDescription = "";
        $titleMeta = "";
        $prefix = "/";
        if (getLanguage() == 'it') {
            $name = $row['nome'];
            $description = $row['meta_description'];
            $courseDescription = $row['descrizione'];
            $keywords = $row['meta_keywords'];
            $title = $row['titolo'];
            $descr = $row['descr'];
            $titleMeta = "Corso di Master di Cucina";
        } else {
            $name = $row['nome_en'];
            $description = $row['meta_description_en'];
            $keywords = $row['meta_keywords_en'];
            $title = $row['titolo_en'];
            $courseDescription = $row['descrizione_en'];
            $descr = $row['descr_en'];
            $titleMeta = "Masters – School of Culinary Arts";
            $prefix = "/en/";
        }
        
        $meta["title"] = $name . "|" . $titleMeta;
        $meta["description"] = $description;
        $meta["keywords"] = $keywords;
        $myTemplate->setMeta($meta);
        $coursename = $name;
        $myTemplate->setBodyContentString("corso", $coursename);
        $myTemplate->setBodyContentString("titolo", $title);
        $ore = $row['ore'];
        $myTemplate->setBodyContentString("ore", $ore);
        $possplit = 0;
        $letter_one = substr($courseDescription, 0, 1);
        if ($letter_one == '&') {
            $possplit = strpos($courseDescription, ';') + 1;
            $letter_one = substr($courseDescription, 0, $possplit + 1);
        }
        $descrizione = substr($courseDescription, $possplit + 1);
        $myTemplate->setBodyContentString("prima_lettera", $letter_one);
        $myTemplate->setBodyContentString("descrizione", $descrizione);
        $myTemplate->setBodyContentString("descr", $descr);
        $myTemplate->setBodyContentImage("corso_avatar", $row['img']);
        $myTemplate->setBodyContentString("professore", $row['professore']);
        $myTemplate->setBodyContentString("email", $row['email']);
        $myTemplate->setBodyContentString("titolo_professore", $row['ptitolo']);
        
        require_once 'include/language_manager.inc.php';
        Language_Manager::getInstance();
        $myTemplate->setBodyContentString("nav_page", L::messages_course_nav_page . $name);
        $myTemplate->setBodyContentString("nav_message", L::nav_message);
    } else {
        $ok = false;
    }
    
    $query = "select `a`.`id` AS `id`,`c`.`nome` AS `nome_corso`,`c`.`nome_en` AS `nome_corso_en`,`a`.`nome` AS `nome`,`a`.`value` AS `value` from (`abilita` `a` join `corsi` `c`) where (`a`.`id_corso` = `c`.`id`) and " . $where;
    if ($ok && ($data = DB_Manager::getInstance()->query($query))) {
        $cnt = 1;
        while ($row = $data->fetch_array(MYSQLI_ASSOC)) {
            $myTemplate->setBodyContentString("num", $cnt++);
            $orec = $row['value'];
            $perc = (100 * $orec) / $ore;
            $myTemplate->setBodyContentString("val", $perc);
            $myTemplate->setBodyContentString("orec", $orec);
            $myTemplate->setBodyContentString("impegno", $row['nome']);
        }
    } else {
        $ok = false;
    }
    $myTemplate->setCanonicalUrl($prefix . "master/" . $_REQUEST['name'] . "/");
} else {
    $ok = false;
}
if (!$ok) {
    redirect($page = "master");
}

